package com.example.administrator.jokeyboarddemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.jokeyboarddemo.keyboard.KeyBoardCallBack;
import com.example.administrator.jokeyboarddemo.keyboard.KeyBoardType;
import com.example.administrator.jokeyboarddemo.keyboard.KeyboardView;


public class MathActivity extends Activity implements View.OnClickListener {

    /**
     * 主要问题
     * 数学键盘中的布局切换方式,多种布局如何实现切换
     * 自定义输入法弹出如何保证高度合适
     * addView 添加布局先后顺序问题
     * @param savedInstanceState
     */


    private Button btn_showkeyboard;
    private KeyboardView keyboardView;

    private Button btnMath;
    private Button btnChinese;
    private Button btnEnglish;


    private EditText edit_mathkeyboard;
    private EditText edit_mathkeyboard2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_math);
        initView();
        initEditTextOnClick();

    }

    private void initView() {
        findViewById(R.id.btn_showkeyboard).setOnClickListener(this);
        findViewById(R.id.tv_gone_keyboard).setOnClickListener(this);
        findViewById(R.id.btn_math_keyboard).setOnClickListener(this);
        findViewById(R.id.btn_chinese_keyboard).setOnClickListener(this);
        findViewById(R.id.btn_english_keyboard).setOnClickListener(this);
        btnMath = (Button)findViewById(R.id.btn_math_keyboard);
        btnChinese = (Button)findViewById(R.id.btn_chinese_keyboard);
        btnEnglish = (Button)findViewById(R.id.btn_english_keyboard);
        btn_showkeyboard = (Button) findViewById(R.id.btn_showkeyboard);
        keyboardView = (KeyboardView) findViewById(R.id.layout_keyboard);
        edit_mathkeyboard = (EditText) findViewById(R.id.edit_mathkeyboard);
        edit_mathkeyboard2 = (EditText) findViewById(R.id.edit_mathkeyboard2);

        keyboardView.setOnClickKeyFinishListener(new KeyBoardCallBack() {
            @Override
            public void getFinishEditValue(String text) {
                edit_mathkeyboard.getText().append(text);
            }

            @Override
            public void deleteKeyBoard() {
                int currentSelect = edit_mathkeyboard.getSelectionStart();
                if(edit_mathkeyboard.getText().length()!=0){
                    edit_mathkeyboard.getText().delete(currentSelect-1,currentSelect);
                }
            }

            @Override
            public void deleteLongKeyBoard() {
            }

            @Override
            public void onNext() {

            }

            @Override
            public void onLast() {

            }

            @Override
            public void closeKeyBoard() {

            }

            @Override
            public void showMolecular() {

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_showkeyboard:
                if (keyboardView.getVisibility() == View.GONE) {
                    keyboardView.setVisibility(View.VISIBLE);
                    btn_showkeyboard.setText("隐藏键盘");

                } else {
                    keyboardView.setVisibility(View.GONE);
                    btn_showkeyboard.setText("显示键盘");
                }
                break;
            case R.id.btn_chinese_keyboard:
                keyboardView.showedBoardView(KeyBoardType.CHINESE);
                break;
            case R.id.btn_english_keyboard:
                keyboardView.showedBoardView(KeyBoardType.ENGLISH);
                break;
            case R.id.btn_math_keyboard:
                keyboardView.showedBoardView(KeyBoardType.MATH);
                break;
        }
    }

    private void initEditTextOnClick() {
        edit_mathkeyboard.setOnClickListener(keyboardEditTextOnClick);
        edit_mathkeyboard2.setOnClickListener(keyboardEditTextOnClick);

    }

    private void isShowKeyboardView() {
        if (keyboardView.getVisibility() == View.GONE) {
            keyboardView.setVisibility(View.VISIBLE);

        }
    }

    /**
     * EditText绑定事件
     */

    View.OnClickListener keyboardEditTextOnClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            isShowKeyboardView();
            switch (v.getId()) {
                case R.id.edit_mathkeyboard:

                    break;
                case R.id.edit_mathkeyboard2:
                    break;
            }
        }
    };



}